
import TSObjectPool from "./TSObjectPool";
import KeyAble from "../interface/KeyAble";


export default class TSPoolManager  {
    //对象池表
    private pools = {}


    private static ins:TSPoolManager;

    static instance():TSPoolManager{
        if(!this.ins){
            this.ins = new TSPoolManager();
        }
        return this.ins;
    }


    init<T extends KeyAble>(key:string,type: { new(): T ;} ,count:number = 1): void {
        if(!this.pools[key]){
            this.pools[key] = new TSObjectPool(key,type,count);
        }
    }
    /**
     * 获得被销毁的对象
     * @param key 
     */
    get<T extends KeyAble>(key:string,type: { new(): T ;},count: number = 1 ): T {
        if(!this.pools[key]){
            this.pools[key] = new TSObjectPool(key,type,count);
        }
        return this.pools[key].get(type);
    }   
    
    put(instance:KeyAble){
        let pool = this.pools[instance.getKey()]
        if(pool){
            pool.put(instance);
        }
    }
}
